﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WechatModel;
using HttpUtility;
using WechatUtility;
using NS.Utilities;
using NS.BLL.Systems;

namespace WechatDAL
{
    public class JSTickenDal
    {
        /// <summary>
        /// 计算JS接口签名
        /// </summary>
        /// <param name="wt">0公众号，1企业号</param>
        /// <param name="url">页面url</param>
        /// <param name="appid"></param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="noncestr">随机字符串</param>
        /// <returns></returns>
        public string GetJSSign(int wt, string url, out string appid, out string timestamp, out string noncestr)
        {
            timestamp = "";
            noncestr = "";
            appid = "";
            string ticket = wt == 0 ? GetPublicJSTicket() : GetEntJSTicket();
            if (string.IsNullOrEmpty(ticket))
                return "";

            appid = wt == 0 ? WechatAPI4Public.AppID : WechatAPI4Ent.CorpID;
            timestamp = Filter.TimeHelper.GetTimeStamp().ToString();
            noncestr = Filter.JSFilter.RndString(15);
            string str = string.Format("{0}={1}&{2}={3}&{4}={5}&{6}={7}",
                "jsapi_ticket", ticket, "noncestr", noncestr, "timestamp", timestamp, "url", url);
            str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower();
            return str;
        }

        /// <summary>
        /// 企业号获取网页JS接口授权Ticket
        /// </summary>
        /// <returns></returns>
        public string GetEntJSTicket()
        {
            return GetMetaConfig("EntJSTicket");
        }

        /// <summary>
        /// 公众号获取网页JS接口授权Ticket
        /// </summary>
        /// <returns></returns>
        public string GetPublicJSTicket()
        {
            return GetMetaConfig("PublicJSTicket");
        }

        /// <summary>
        /// 获取JSTicket
        /// </summary>
        /// <param name="key">表示公众号或服务号：带Public=公众号，带Ent=服务号</param>
        /// <returns></returns>
        private string GetMetaConfig(string key)
        {
            string jSTicket = DictionaryService.GetDicValue("微信参数", "JSTicket");
            DateTime timeOut = DateTime.Parse(DictionaryService.GetDicValue("微信参数","TicketTimeOut"));
            if (DateTime.Now >= timeOut)  //如果已过期，则获取新的JSTicket
            {
                JSTicketInfo info = GetNewJSSDKTicket(key);
                if (info != null)
                {
                    jSTicket = info.Ticket;
                    timeOut = info.TimeOut;
                    DictionaryService.UpdateDicValue("微信参数", "JSTicket", jSTicket);
                    DictionaryService.UpdateDicValue("微信参数", "TicketTimeOut", timeOut.ToString("yyyy-MM-dd HH:mm:ss"));
                }
            }

            return jSTicket;
        }

        /// <summary>
        /// 获取网页JS接口授权Ticket
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private JSTicketInfo GetNewJSSDKTicket(string key)
        {
            string result = "";
            switch (key)
            {
                case "PublicJSTicket":
                    result = Poster.DoGet(WechatAPI4Public.GetJSTicket);
                    break;
                case "EntJSTicket":
                    result = Poster.DoGet(WechatAPI4Ent.GetJSTicket);
                    break;
            }
            if (string.IsNullOrEmpty(result))
                return null;
            else
                return JsonHelper.JSON2T<JSTicketInfo>(result); ;
        }
    }
}
